package live.hotel.dao.impl;

import java.util.Iterator;
import java.util.List;

import live.hotel.dao.IXmlManage;
import live.hotel.entity.HotelInfo;

import org.dom4j.Document;
import org.dom4j.Element;


@SuppressWarnings("unchecked")
public class HotelList {
	IXmlManage xmlMan = null;

	public List<HotelInfo> getHotelList(String cityCode) throws Exception {
		xmlMan = new XmlManage();
		Document doc = xmlMan.getDataFromApi(this.resturnRequestXml(cityCode));
		List list = null;
		List hotes = doc.selectNodes("//ItemDetails/*");
		if (hotes != null && hotes.size() > 0) {
			Iterator iter = hotes.iterator();
			while (iter.hasNext()) {
				Element e = (Element) iter.next();
				HotelInfo info = new HotelInfo();
				info.setCode(e.element("Item").attributeValue("Code"));
				info.setItem(e.elementTextTrim("Item"));
				list.add(info);
			}
		}
		return list;
	}

	public String resturnRequestXml(String cityCode) {
		StringBuffer buffer = new StringBuffer(
				"<SearchItemRequest ItemType=\"hotel\">\n").append(
				"<ItemDestination DestinationType=\"city\" DestinationCode=\""
						+ cityCode + "\" />\n")
				.append("</SearchItemRequest>\n").append("</RequestDetails>\n")
				.append("</Request>\n");
		return buffer.toString();
	}
}
